package com.aiju.cash.ui.stockupdate.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.stockupdate.bean.Stock;
import com.aiju.cash.util.KeywordUtil;
import com.aiju.common.image.AiJuImageLoaderProxy;
import com.aiju.utils.StringUtil;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.aiju.utils.Util.getResources;

/**
 * 作者：流冥 on 2017/7/3 10:53
 * 说明：
 */

public class StockAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    private List<Stock.GoodsListBean> contentList = new ArrayList<>();
    private String keyWord;

    public StockAdapter(List<Stock.GoodsListBean> dataList) {
        contentList = dataList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_stock_list_content, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MyViewHolder myViewHolder = (MyViewHolder) holder;
        if (position >= contentList.size()) {
            return;
        }
        final Stock.GoodsListBean goodsListBean = contentList.get(position);
        AiJuImageLoaderProxy.getInstance().displayImage(goodsListBean.getImageUrl(), myViewHolder.imageView, R.mipmap.icon_empty);
        if (StringUtil.isBlank(keyWord)) {
            myViewHolder.title.setText(goodsListBean.getName());
            myViewHolder.subTitle.setText(goodsListBean.getCode());
        } else {
            myViewHolder.title.setText(KeywordUtil.matcherSearchTitle(getResources().getColor(R.color.app_display_color), goodsListBean.getName(), keyWord));
            myViewHolder.subTitle.setText(KeywordUtil.matcherSearchTitle(getResources().getColor(R.color.app_display_color), goodsListBean.getCode(), keyWord));
        }
        myViewHolder.stock_num.setText("库存: " + goodsListBean.getSurplus());
        myViewHolder.saled_num.setText("昨日已售: " + goodsListBean.getYesterdayNum());
        myViewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClickListener(goodsListBean);
            }
        });
    }


    @Override
    public long getHeaderId(int position) {
       /* L.d(position);*/
        //得到headId 唯一性
        return contentList.get(position).getGoodsTypeName().hashCode();
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_stock_list_header, parent, false);
        return new MyHeaderViewHolder(view);

    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyHeaderViewHolder myHeaderViewHolder = (MyHeaderViewHolder) holder;
        myHeaderViewHolder.title.setText(contentList.get(position).getGoodsTypeName());
    }

    @Override
    public int getItemCount() {
        return contentList.size();
    }

    //必须重写  不然item会错乱
    @Override
    public long getItemId(int position) {
        return position;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView title;
        public TextView subTitle;
        public TextView stock_num;
        public TextView saled_num;
        public View rootView;

        public MyViewHolder(View view) {
            super(view);
            imageView = (ImageView) view.findViewById(R.id.image);
            title = (TextView) view.findViewById(R.id.title);
            subTitle = (TextView) view.findViewById(R.id.sub_title);
            stock_num = (TextView) view.findViewById(R.id.stock_num);
            saled_num = (TextView) view.findViewById(R.id.saled_num);
            rootView = (View) view.findViewById(R.id.parent_view);
        }
    }// item

    public static class MyHeaderViewHolder extends RecyclerView.ViewHolder {

        TextView title;

        public MyHeaderViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.head_name);
        } //head
    }

    public void setList(List<Stock.GoodsListBean> list, String keyWord) {
        this.keyWord = keyWord;
        contentList = list;
        notifyDataSetChanged();
    }

    public interface StockListItemClickListener {
        void onItemClickListener(Stock.GoodsListBean goodsListBean);
    }

    private StockListItemClickListener mListener;

    public void setListener(StockListItemClickListener listener) {
        mListener = listener;
    }

}
